package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2023-05-14
 * Time: 17:51
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int sorce;

    public Student(String name, int age, int sorce) {
        this.name = name;
        this.age = age;
        this.sorce = sorce;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sorce=" + sorce +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        /*if(this.age > o.age){
            return 1;
        }else if(this.age < this.age){
            return -1;
        }else{
            return 0;
        }*/
        return this.age - o.age;
    }
}
/*class Student {
    public String name;
    public int age;
    public int sorce;

    public Student(String name, int age, int sorce) {
        this.name = name;
        this.age = age;
        this.sorce = sorce;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sorce=" + sorce +
                '}';
    }

}*/
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.sorce - o2.sorce;
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",15,57);

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));

        //比较器1
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));

        //比较器2
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students,scoreComparator);
        System.out.println(Arrays.toString(students));

        //比较器3
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println(Arrays.toString(students));
    }
    /*public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",15,57);

        *//*if(students[0].compareTo(students[1]) > 0){
            System.out.println("students[0] > students[1]");
        }*//*
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }*/
}
